<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Laravel\Socialite\Facades\Socialite;
use App\Models\System\User;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Auth;

class LoginQqController extends Controller
{
    public function qq()
    {
        return Socialite::with('qq')->redirect();
    }

    public function qqlogin()
    {
        $user = Socialite::driver('qq')->user();
        $check = User::where('uid', $user->id)->where('provider', 'qq_connect')->first();

        if (!$check) {
            $customer = User::create([
                'uid' => $user->id,
                'provider' => 'qq_connect',
                'name' => $user->nickname,
                'email' => 'qq_connect+' . $user->id . '@example.com',
                'password' => bcrypt(Str::random(60)),
                'image' => $user->avatar
            ]);
        } else {
            //如果数据库中已经有了当前用户
            $customer = $check;
        }

        //记住我
        Auth::login($customer, true);
        return redirect('/home');
    }
}
